import { request } from '../../request/index'

Page({
    data: {
        isFocus: false,
        inputValue: "", ///关键字
        searchGoods: [] /// 搜素数据
    },
    TimeId: -1,
    handleInputVal(e) {
        const { value } = e.detail
        console.log(e.detail)
        /// 无输入不显示取消按钮，否则显示为true
        if (!value.trim()) {
            this.setData({
                searchGoods: [],
                isFocus: false
            })
            return
        }
        this.setData({
            isFocus: true
        })
        /// 清除定时器缓存
        clearTimeout(this.TimeId)
        /// 防抖
        this.TimeId = setTimeout(() => {
            this.getGoodsSearch(value)
        }, 1000)
    },
    async getGoodsSearch(query) {
        const res = await request({
            url: '/goods/qsearch',
            data: {
                query
            }
        })
        this.setData({
            searchGoods: res
        })
        console.log(res, 'search');
    },
    /// 点击取消
    handleCancel() {
        this.setData({
            inputValue: "",
            isFocus: false,
            searchGoods: []
        })
    }
})